﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Controls;
using HR60_PanelSoft.View.Base;

namespace HR60_PanelSoft.View.TabForms
{
	public partial class PanelForm_Buttons : BaseForm
	{
		public PanelForm_Buttons()
		{
			InitializeComponent();
		}

		// 绑定数据
		public override void refreshFormData(System.Collections.Generic.IDictionary<string, object> dataMap)
		{
			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)dataMap["panel"];

			if (panel.type.maxPage > 0)
			{
				btnPage.PageCount = panel.type.maxPage;
				btnPage.ButtonCountPerPage = panel.type.buttonsPerPage;
				refreshButtonTable(1);
				btnPage.Visible = true;
			}
			else
			{
				btnPage.Visible = false;
			}


			if (panel.type.maxScenePage > 0)
			{
				scenePage.PageCount = panel.type.maxScenePage;
				scenePage.ButtonCountPerPage = panel.type.buttonsPerPage;
				refreshSceneTable(1);
				scenePage.Visible = true;
			}
			else
			{
				scenePage.Visible = false;
			}

		}

		// 数据修改事件
		public override void onFormChanged(BaseFormControl sender, System.EventArgs e)
		{
			// 按钮影射修改
			if (sender == btnMappingEdit)
			{
				object val = btnMappingEdit.Value;
				if (val == null)
				{
					// TODO:
					btnPage.setTableCellText(btnPage.SelectedButtonNum, "未定义");
				}
				else if (val.GetType().Equals(typeof(Device)))
				{
					// TODO:
					btnPage.setTableCellText(btnPage.SelectedButtonNum, "[设备]" + ((Device)val).nickName);
				}
				else if (val.GetType().Equals(typeof(Scene)))
				{
					// TODO:
					btnPage.setTableCellText(btnPage.SelectedButtonNum, "[场景]" + ((Scene)val).name);
				}
				
			}

			// 场景按钮影射修改
			else if (sender == btnSceneMappingEdit)
			{
				Scene val = (Scene) btnSceneMappingEdit.Value;
				if(val == null)
				{
					scenePage.setTableCellText(scenePage.SelectedButtonNum, "未定义");
				}
				else
				{
					scenePage.setTableCellText(scenePage.SelectedButtonNum, val.name);
				}
			}
		}

		// 联动按键表格
		private void refreshButtonTable(int pageNum)
		{
			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];

			for (int i = 0; i < btnPage.ButtonCountPerPage; i++)
			{
				PanelButton btn = panel.pages[pageNum - 1].buttons[i];
				if (btn.mappingDevice != null)
				{
					btnPage.setTableCellText(i + 1, "[设备]" + findDeviceByUuid(btn.mappingDevice).nickName);
				}
				else if (btn.mappingScene != null)
				{
					btnPage.setTableCellText(i + 1, "[场景]" + findSceneByUuid(btn.mappingScene).name);
				}
				else
				{
					btnPage.setTableCellText(i + 1, "未定义");
				}
			}
		}

		// 联动场景表格
		private void refreshSceneTable(int pageNum)
		{
			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];

			for (int i = 0; i < btnPage.ButtonCountPerPage; i++)
			{
				PanelButton btn = panel.pages[pageNum - 1].buttons[i];
				if (btn.mappingScene != null)
				{
					scenePage.setTableCellText(i + 1, "[场景]" + findSceneByUuid(btn.mappingScene).name);
				}
				else
				{
					scenePage.setTableCellText(i + 1, "未定义");
				}
			}
		}

		// 按键页面翻页
		private void btnPage_PageChanged(object sender, EventArgs e)
		{
			isBindDataFlag = false;
			refreshButtonTable(btnPage.PageNum);
			isBindDataFlag = true;
		}

		// 场景页面翻页
		private void scenePage_PageChanged(object sender, EventArgs e)
		{
			isBindDataFlag = false;
			refreshSceneTable(scenePage.PageNum);
			isBindDataFlag = true;
		}

		// 按钮页按钮选择
		private void btnPage_ButtonSelected(object sender, EventArgs e)
		{
			isBindDataFlag = false;
			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];

			if (btnPage.SelectedButtonNum == 0)
			{
				btnMappingEdit.bindButton(null);
			}
			else
			{
				PanelButton btn = panel.pages[btnPage.PageNum - 1].buttons[btnPage.SelectedButtonNum - 1];
				btnMappingEdit.Title = "按钮" + btnPage.SelectedButtonNum;
				btnMappingEdit.bindButton(btn);
			}
			isBindDataFlag = true;
		}

		// 场景页按钮选择
		private void scenePage_ButtonSelected(object sender, EventArgs e)
		{
			isBindDataFlag = false;
			HR60_PanelSoft.Model.Panel panel = (HR60_PanelSoft.Model.Panel)bindDataMap["panel"];

			if (scenePage.SelectedButtonNum == 0)
			{
				btnSceneMappingEdit.bindButton(null);
			}
			else
			{
				PanelButton btn = panel.scenePages[scenePage.PageNum - 1].buttons[scenePage.SelectedButtonNum - 1];
				btnSceneMappingEdit.Title = "按钮" + scenePage.SelectedButtonNum;
				btnSceneMappingEdit.bindButton(btn);
			}
			isBindDataFlag = true;
		}

	}
}
